//
//  AppController.m
//  Timeatron
//
//  Created by Kumaran Vijayan on 11/12/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "AppController.h"


@implementation AppController
-(IBAction)start:(id)sender {
	//Fire edit: to ensure that window is out of edit mode
	[self edit:sender];
	[editTime setState:0];
	
	//Set the title of the button to stop and its corresponding functionality
	[sender setTitle:@"Stop"];
	[sender setAction:@selector(stop:)];
	
	//Turn off edit button when timer is started
	[editTime setEnabled:NO];
	
	//Set the timer and call startTimer
	timeInterval = 3;
	workTimer = [NSTimer timerWithTimeInterval:timeInterval target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];
	[self startTimer];
	
}

-(IBAction)stop:(id)sender {
	//Set the title of the button to start and its corresponding functionality
	[sender setTitle:@"Start"];
	[sender setAction:@selector(start:)];
	
	//Invalidate workTimer in order to stop the timer
	[workTimer invalidate];
	
	//Turn edit button back on when timer is stopped
	[editTime setEnabled:YES];
}

-(IBAction)edit:(id)sender {
	//Get the frame of the window before it has resized
	NSRect windowFrame = [NSWindow contentRectForFrameRect:[[sender window] frame] styleMask:[[sender window] styleMask]];
	if ([[sender window] frame].size.height > 190) {
		//Make a new rect for the new frame
		//Increase the origin.y by 100 and decrease size.height by 100
		NSRect newWindowFrame = [NSWindow frameRectForContentRect:NSMakeRect(windowFrame.origin.x, windowFrame.origin.y+100, windowFrame.size.width, windowFrame.size.height-100) styleMask:[[sender window] styleMask]];
		//Set the window frame to the newWindowFrame
		[[sender window] setFrame:newWindowFrame display:YES animate:YES];
	} else {
		//Make a new rect for the new frame
		//Decrease the origin.y by 100 and increase size.height by 100
		NSRect newWindowFrame = [NSWindow frameRectForContentRect:NSMakeRect(windowFrame.origin.x, windowFrame.origin.y-100, windowFrame.size.width, windowFrame.size.height+100) styleMask:[[sender window] styleMask]];
		//Set the window frame to the newWindowFrame
		[[sender window] setFrame:newWindowFrame display:YES animate:YES];
	}
}

-(void)startTimer {
	//Add the workTimer to the mainRunLoop
	[[NSRunLoop mainRunLoop] addTimer:workTimer forMode:NSDefaultRunLoopMode];
}

-(void)timerFire:(NSTimer*)theTimer {
	//Make the sound and using NSSound and play it
	NSSound* fireSound = [NSSound soundNamed:@"Glass"];
	[fireSound play];
}
@end
